<?php

/**

 * This is the model class for table "{{blocks}}".

 *

 * The followings are the available columns in table '{{block}}':

 * @property integer $id

 * @property string $key

 * @property string $value

 * @property string $type

 * @property string $position

 * @property string $extra

 * @property integer $orders

 * @property integer $create_time

 * @property integer $update_time

 */
class AdBlock extends CActiveRecord {
    /**

     * Returns the static model of the specified AR class.

     * @param string $className active record class name.

     * @return Block the static model class

     */

    const ACTIVE = 1;
    const DISABLE = 0;

    public static function model($className = __CLASS__) {

	return parent::model($className);
    }

    /**

     * @return string the associated database table name

     */
    public function tableName() {

	return '{{ad_blocks}}';
    }

    /**

     * @return array validation rules for model attributes.

     */
    public function rules() {

	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.

	return array(
	    array(' params, type, position', 'required'),
	    array('expired, created, updated, status', 'numerical', 'integerOnly' => true),
	    array('type', 'length', 'max' => 15),
	    array('position', 'length', 'max' => 45),
	    array('params', 'safe'),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('block_id, type, position, params, expired, created, updated, status', 'safe', 'on' => 'search'),
	);
    }

    /**

     * @return array relational rules.

     */
    public function relations() {

	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.

	return array(
	);
    }

    /**

     * @return array customized attribute labels (name=>label)

     */
    public function attributeLabels() {

	return array(
	    'block_id' => 'ID',
	    'params' => 'Value',
	    'type' => 'Type',
	    'position' => 'Position',
	    'created' => 'Create Time',
	    'updated' => 'Update Time',
	    'expired' => 'Expired Time',
	    'status' => 'Status'
	);
    }

    /**

     * Retrieves a list of models based on the current search/filter conditions.

     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

     */
    public function search() {

	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;


	$criteria->compare('block_id', $this->block_id);

	$criteria->compare('type', $this->type, true);

	$criteria->compare('position', $this->position, true);

	$criteria->compare('params', $this->params);

	$criteria->compare('expired', $this->expired);

	$criteria->compare('created', $this->created);

	$criteria->compare('updated', $this->updated);
	$criteria->compare('status', $this->status);
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

    /**

     * Retrieves a list of models based on the current search/filter conditions.

     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

     */
    public function searchAds() {

	// Warning: Please modify the following code to remove attributes that
	// should not be searched.



	$criteria = new CDbCriteria;

	//$criteria->condition = "t.key LIKE '%_ads%'";



	$criteria->compare('block_id', $this->block_id);

	$criteria->compare('type', $this->type, true);

	$criteria->compare('position', $this->position, true);

	$criteria->compare('params', $this->params);

	$criteria->compare('expired', $this->expired);

	$criteria->compare('created', $this->created);

	$criteria->compare('updated', $this->updated);
	$criteria->compare('status', $this->status);


	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

    public function beforeSave() {

	if (parent::beforeSave()) {

	    if ($this->isNewRecord) {

		$this->created = $this->updated = time();
	    } else {

		$this->updated = time();
	    }



	    return true;
	}

	return false;
    }

    public function instantiate($attributes) {

	switch ($attributes['type']) {

	    case 'google_ads' :

		$class = 'GoogleAds';

		break;

	    case 'flash_ads':

		$class = 'FlashAds';

		break;
	    case 'image_ads':default:

		$class = 'ImageAds';

		break;

	    default:
		$class = 'AdBlock';
		break;
	}



	$model = new $class;

	return $model;
    }

    /**

     * return type of ads base on type attribute

     */
    public function getAdType() {

	switch ($this->type) {

	    case 'google_ads':

		$return = 'Google Ads';

		break;

	    case 'flash_ads':

		$return = 'Flash Ads';

		break;
	    case 'image_ads':

		$return = 'Image Ads';

		break;

	    default :

		$return = 'Undefine';

		break;
	}



	return $return;
    }

    /**

     *

     */
    public function getUpdateUrl() {

	switch ($this->type) {
	    case 'google_ads':
		$return = Yii::app()->createUrl('/ad/googleads/update', array('id' => $this->block_id));
		break;
	    case 'flash_ads':
		$return = Yii::app()->createUrl('/ad/flashads/update', array('id' => $this->block_id));
		break;
	    case 'image_ads':
		$return = Yii::app()->createUrl('/ad/imageads/update', array('id' => $this->block_id));
		break;
	    default :
		$return = 'Undefine';
		break;
	}
	return $return;
    }

    /**
     *
     */
    public function getDeleteUrl() {
	switch ($this->type) {
	    case 'google_ads':
		$return = Yii::app()->createUrl('/ad/googleads/delete', array(' id' => $this->block_id));
		break;
	    case 'flash_ads':
		$return = Yii::app()->createUrl('/ad/flashads/delete', array('id' => $this->block_id));
		break;
	    case 'image_ads':
		$return = Yii::app()->createUrl('/ad/imageads/delete', array('id' => $this->block_id));
		break;
	    default :
		$return = 'Undefine';
		break;
	}

	return $return;
    }

    /**
     * get ad position
     * @return string
     */
    public function getPosition() {

	switch ($this->position) {
	    case 'home_right' :
		$return = 'Home page';
		break;

	    case 'bottom' :
		$return = 'bottom';
		break;

	    case 'left' :
		$return = 'Left sidebar';
		break;

	    case 'right' :
		$return = 'Right sidebar';
		break;
	    case 'top' :
		$return = 'Top';
		break;
	    default:
		$return = 'Undefine';

		break;
	}

	return $return;
    }

}

